-- add_attrib_mon
-- create by wangy
-- 技能: 增加全体怪物（攻击、闪避）属性，根据威力和攻击计算


return {
    apply = function(source, target, skillId, round, para, extra)
        -- 必须是怪物
        if source.type ~= OBJECT_TYPE_MONSTER then
            return;
        end

        -- 同一种属性，每回合只能发动一次
        local roar_action = source.dbase:query("roar_action") or {};
        roar_action[para[1]] = roar_action[para[1]] or 0;
        if roar_action[para[1]] >= round then
            return;
        end

        -- 威力
        local force = para[2];
        local attrib = para[1];

        -- 遍历所有的怪
        local ret = {};

        for p = 1, DUNGEON_WIDTH * DUNGEON_HEIGHT do
            local grid = DungeonM.getGridByPos(p);

            -- 是未死亡的怪物
            if  grid:isOpened() and
                grid:isMonster() and
                not grid.monster:isDead() then
                table.insert(ret, grid.monster);
            end
        end

        -- 减弱咆哮效果
        local p = PropM.combine(ME.user, "reduce_negative", "roar");

        local value;
        for _, monster in pairs(ret) do
            -- 血量的处理
            if attrib == "hp" then
                local max = monster:getMaxHp();
                value = math.modf(max * force / 1000);
                -- 减弱咆哮效果
                value = PropM.apply(p, value);
                if value <= 0 then
                    return 0;
                end
                if monster:queryAttrib("hp") > 0 then
                    CombatM.receiveCure(source, monster, value);
                end
            else
                value = math.modf(monster.dbase:query(attrib, 0) * force / 1000);
                -- 减弱咆哮效果
                value = PropM.apply(p, value);
                if value <= 0 then
                    return 0;
                end
                -- 增加属性，记在怪物身上
                local propId = PropM.getPropId("attrib2");
                local prop = monster.dbase:queryTemp("prop", "add_attrib_mon", {});
                if #prop > 0 then
                    value = prop[1][3] + value;
                end
                prop = { propId, attrib, value, -1, };
                monster.dbase:setTemp("prop", "add_attrib_mon", { prop, });

                -- 重整一次属性
                PropM.arrangeProps(monster);
            end
        end

        -- 记录最近一次咆哮
        roar_action[attrib] = round;
        source.dbase:set("roar_action", roar_action);

        return ret;
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { arr[1], tonumber(arr[2]), };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        -- 1. 威力
        local force = para[1];

        -- 最终威力
        local prop = PropM.combine(source, "magic_effect", skillId);
        force = force + PropM.apply(prop, force);

--        -- 某系技能威力加成
--        prop = PropM.combine(source, "magic_effect_style", SkillM.query(skillId, "style"));
--        force = PropM.apply(prop, force);
--
--        -- 恢复类魔法效果加成
--        prop = PropM.combine(source, "magic_effect_style", "cure");
--        force = PropM.apply(prop, force);

        -- 2. 施展方的魔力
        local magic = source:getMagic();

        -- 3. 作用/计算时魔力+3
        local hp = math.modf(force * (magic + 3) / 1000);

        desc = string.gsub(desc, "{cure}", hp);

        return desc;
    end,
}

